package com.fosdata.dpm.services.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fosdata.dpm.domain.SysUser;
import com.fosdata.dpm.services.mapper.SysUserMapper;
import com.fosdata.dpm.services.SysUserServices;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author lqx
 * @since 2025-10-19
 */
@Service
public class SysUserServicesImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserServices {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser queryuserAndpasswd(String phone, String password) {

        String passd = SecureUtil.md5(password);
        QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("phone",phone).eq("password",passd).eq("status","1");

        SysUser user = this.sysUserMapper.selectOne(userQueryWrapper);
        if (user !=null){
            return user;
        }
        return null;
    }

    @Override
    public int addSysUser(String username, String passwd, String nickname, String phone, String email) {

        try {
            QueryWrapper<SysUser> sysUserQueryWrapper= new QueryWrapper<>();
            sysUserQueryWrapper.eq("phone",phone);
            SysUser user = this.baseMapper.selectOne(sysUserQueryWrapper);
            if (user== null){
                String passMd5 = SecureUtil.md5(passwd);
                user =new SysUser();
                user.setUsername(username);
                user.setPassword(passMd5);
                user.setNickname(nickname);
                user.setPhone(phone);
                user.setEmail(email);
                user.setStatus(1);
               return  this.baseMapper.insert(user);
            }
        }catch (Exception e){
                e.printStackTrace();
        }

        return 0;
    }
}
